package com.shierke.Ume.ui.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.jph.takephoto.app.TakePhoto
import com.jph.takephoto.app.TakePhotoImpl
import com.jph.takephoto.compress.CompressConfig
import com.jph.takephoto.model.CropOptions
import com.jph.takephoto.model.InvokeParam
import com.jph.takephoto.model.TContextWrap
import com.jph.takephoto.model.TResult
import com.jph.takephoto.permission.InvokeListener
import com.jph.takephoto.permission.PermissionManager
import com.jph.takephoto.permission.TakePhotoInvocationHandler
import com.shierke.Ume.R
import com.shierke.Ume.base.BaseActivity
import com.shierke.Ume.ui.adapter.explore.ExplorePhotoAdapter
import com.shierke.Ume.ui.view.SpaceItemDecoration
import com.shierke.Ume.util.click
import com.shierke.Ume.util.startActivity
import kotlinx.android.synthetic.main.activity_add_trends.*
import java.io.File

/**
 * Create by Ybx
 * on 2020-10-12
 * Desribe:
 */
class AddTrendsActivity : BaseActivity(), TakePhoto.TakeResultListener, InvokeListener {


    override fun takeSuccess(result: TResult?) {

        val compressPath = result!!.image.compressPath
        explorePhotoAdapter.getPhotoList().add(compressPath)
        explorePhotoAdapter.notifyDataSetChanged()
    }

    override fun takeCancel() {
    }

    override fun takeFail(result: TResult?, msg: String?) {

    }

    override fun invoke(invokeParam: InvokeParam?): PermissionManager.TPermissionType {
        val type =
            PermissionManager.checkPermission(TContextWrap.of(this), invokeParam!!.getMethod())
        if (PermissionManager.TPermissionType.WAIT == type) {
            this.invokeParam = invokeParam
        }
        return type
    }


    fun getTakePhoto(): TakePhoto {
        if (takePhoto == null) {
            takePhoto = TakePhotoInvocationHandler.of(this).bind(
                TakePhotoImpl(
                    this,
                    this
                )
            ) as TakePhoto
        }
        //设置压缩规则，最大500kb
        takePhoto!!.onEnableCompress(CompressConfig.Builder().setMaxSize(100 * 1024).create(), true)
        return takePhoto!!

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        getTakePhoto().onActivityResult(requestCode, resultCode, data)

    }


    override fun onSaveInstanceState(outState: Bundle) {
        getTakePhoto().onSaveInstanceState(outState)
        super.onSaveInstanceState(outState)
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        val type =
            PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionManager.handlePermissionsResult(this, type, invokeParam, this)

    }

    private var takePhoto: TakePhoto? = null
    private var invokeParam: InvokeParam? = null


    companion object {
        fun startActivity(context: Context) {
            context.startActivity<AddTrendsActivity>()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_add_trends)



        setActivityRightText("Post")

        explorePhotoAdapter = ExplorePhotoAdapter {
            val file = File(externalCacheDir, System.currentTimeMillis().toString() + ".png")
            val uri = Uri.fromFile(file)
            val size = Math.min(
                resources.displayMetrics.widthPixels,
                resources.displayMetrics.heightPixels
            )
            val cropOptions =
                CropOptions.Builder().setOutputX(size).setOutputX(size).setWithOwnCrop(true)
                    .create()
            getTakePhoto().onPickFromGalleryWithCrop(uri, cropOptions)
        }


        photoList.layoutManager = GridLayoutManager(this, 3)

        val decoration = SpaceItemDecoration(15)
        photoList.addItemDecoration(decoration)
        photoList.adapter = explorePhotoAdapter



        selectHot.click {
            TrendsChooseHotActivity.startActivity(this)
        }

        location.click {
            TrensChooseLocationActivity.startActivity(this)
        }





    }


    private lateinit var explorePhotoAdapter: ExplorePhotoAdapter

}